<?php

namespace app\warehouse\logic;

use think\facade\Db;

class WarehouseLogic extends BasicLogic
{

    /**
     * @desc   仓库列表
     * @author zhangheng
     * @date   2024-11-30
     */
    public function getWarehouseList($params=[]){
        $limit           = getIsset($params,'limit',10);
        $name             = getIsset($params,'name');
        $where           = [];
        
        if($name){
            $where[] = ['name', 'like', '%' . $name . '%'];
        }
        $res = Db::name('warehouse')
            ->where($where)
            ->order("id desc")
            ->paginate($limit);
        $list = $res->items()?:[];

        foreach($list as &$v){
            switch ($v['status']) {
                case '0':
                    $v['status'] = '初始';
                    break;
                case '1':
                    $v['status'] = '默认仓库';
                    break;
                default:
            }
            
        }
        $data = [
            'list'   => $list,
            'total'  => $res->total(),
        ];
        return  $this->jsonMessage(200,true,'获取成功',$data);
    }

    /**
     * @desc   仓库列表详情
     * @author zhangheng
     * @date   2024-11-30
     */
    public function find($params=[]){
        $id     = getIsset($params,'id');
        $data    = Db::name('warehouse')
            ->field("*")
            ->where('id',$id)
            ->find();
        return  $this->jsonMessage(200,true,'获取成功',$data);
    }

    /**
     * @desc   仓库列表修改
     * @author zhangheng
     * @date   2024-11-30
     */
    public function edit($params=[]){
        $id         = getIsset($params,'id');
        $address    = getIsset($params,'address');
        $admin_id   = getIsset($params,'admin_id');
        $name       = getIsset($params,'name');
        $status     = getIsset($params,'status');
        if(!$name) return   $this->jsonMessage(201,false,'仓库不能为空!',null);
        if(!$id) return     $this->jsonMessage(201,false,'ID不能为空!',null);
        $update = [
            'address'   =>$address,
            'admin_id'  =>$admin_id,
            'name'      =>$name,
            'status'    =>$status,
            'updated_at'        =>time(),
        ];
        Db::name('warehouse')->where(['id'=>$id])->update($update);
        return  $this->jsonMessage(200,true,'操作成功',null);
    }

    /**
     * @desc   仓库列表添加
     * @author zhangheng
     * @date   2024-11-30
     */
    public function add($params=[]){
        $address    = getIsset($params,'address');
        $admin_id   = getIsset($params,'admin_id');
        $name       = getIsset($params,'name');
        $status     = getIsset($params,'status');
        if(!$name) return  $this->jsonMessage(201,false,'仓库不能为空!',null);
        $insert = [
            'address'   =>$address,
            'admin_id'  =>$admin_id,
            'name'      =>$name,
            'status'    =>$status,
            'created_at'        =>time(),
        ];
        Db::name('warehouse')->insert($insert);
        return  $this->jsonMessage(200,true,'操作成功',null);
    }









}